package com.maixy.studyonlineapi.service;

import com.google.common.collect.Lists;
import com.maixy.studyonlineapi.bean.Page;
import com.maixy.studyonlineapi.entity.Activity;
import com.maixy.studyonlineapi.entity.Auth;
import com.maixy.studyonlineapi.entity.Moment;
import com.maixy.studyonlineapi.entity.User;
import com.maixy.studyonlineapi.exceptions.TipException;
import com.maixy.studyonlineapi.mapper.MomentMapper;
import com.maixy.studyonlineapi.result.PageData;
import com.maixy.studyonlineapi.utils.DataTypeUtil;
import com.maixy.studyonlineapi.utils.LocalDateTimeUtils;
import com.maixy.studyonlineapi.view.MomentView;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * @ClassName MomentService
 * @Description TODO
 * @Date 2021/2/7 23:19
 * @Author maixy
 */
@Service
public class MomentService extends BaseMPlusService<Moment, Long>{
    private final MomentMapper momentMapper;

    @Resource
    private ActivityService activityService;
    @Resource
    private AuthService authService;
    @Resource
    private UserService userService;

    protected MomentService(MomentMapper momentMapper) {
        super(Moment.class, momentMapper);
        this.momentMapper = momentMapper;
    }

    /**
     * 添加打卡
     *
     * @param userId 当前用户id
     * @param activityId 课程id
     * @param content 打卡内容
     */
    public void addMoment(Long userId, Long activityId, String content) {
        Activity activity = activityService.findById(activityId);
        if (null == activity || activity.getHasDelete()) {
            throw new TipException("课程不存在或已被删除");
        }
        Auth auth = authService.findAuthByUserIdAndActivityId(userId, activityId);
        if (null == auth || auth.getHasDelete()) {
            throw new TipException("请报名后再打卡");
        }
        Moment moment = new Moment();
        moment.setCreateBy(userId);
        moment.setCreateTime(System.currentTimeMillis());
        moment.setHasDelete(false);
        moment.setActivityId(activityId);
        moment.setContent(content);
        create(moment);
    }

    /**
     * 删除打卡
     *
     * @param userId 当前用户id
     * @param momentId 打卡id
     */
    public void delMoment(Long userId, Long momentId) {
        Moment moment = findById(momentId);
        if (null == moment || moment.getHasDelete()) {
            throw new TipException("打卡内容不存在或已被删除");
        }
        moment.setUpdateBy(userId);
        moment.setUpdateTime(System.currentTimeMillis());
        moment.setHasDelete(true);
        update(moment);
    }

    /**
     * 查看打卡记录
     *
     * @param userId 当前用户id
     * @param activityId 活动id
     * @param hasOnlyMyMoment 是否只看我的打卡
     * @param page 页面信息
     * @return PageData<com.maixy.studyonlineapi.view.MomentView>
     */
    public PageData<MomentView> getMomentList(Long userId, Long activityId, Boolean hasOnlyMyMoment, Page page) {
        page.setHasMore(false);
        Integer beginIndex = (page.getPage() - 1) * page.getSize();
        PageData<MomentView> pageData = new PageData<>();
        pageData.setPage(page);
        pageData.setData(Collections.emptyList());

        List<Moment> momentList = momentMapper.findMomentList(activityId, userId, hasOnlyMyMoment, beginIndex, page.getSize() + 1);
        if (CollectionUtils.isEmpty(momentList)) {
            return pageData;
        }
        if (momentList.size() > page.getSize()) {
            page.setHasMore(true);
            momentList.remove(momentList.size() - 1);
        }
        List<MomentView> momentViewList = Lists.newArrayList();
        for (Moment moment : momentList) {
            MomentView view = new MomentView();
            User user = userService.findById(moment.getCreateBy());
            if (null == user || user.getHasDelete()) {
                continue;
            }
            view.setMomentId(DataTypeUtil.getStr(moment.getId()));
            view.setUserView(userService.convert(user));
            view.setCreateTimeStr(LocalDateTimeUtils.format(moment.getCreateTime(), LocalDateTimeUtils.NORMAL_DATETIME_FORMAT_NO_SECOND));
            view.setContent(moment.getContent());
            momentViewList.add(view);
        }
        pageData.setData(momentViewList);
        return pageData;
    }


}
